import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
const Login = () => import('../components/common/login.vue')
const Welcome = () => import('../views/home/Welcome.vue')
const User = () => import('../views/users/User.vue')
const HomeRoles = () => import('../views/roles/HomeRoles.vue')
const HomeRights = () => import('../views/roles/HomeRights.vue')
const GoodsCategory = () => import('../views/goods/GoodsCategory.vue')
const GoodsLists = () => import('../views/goods/GoodsLists.vue')
const AddGoodsList = () => import('../views/goods/AddGoodsList.vue')
const goodsParams = () => import('../views/goods/goodsParams.vue')
const Orders = () => import('../views/orders/Orders.vue')
const Reports = () => import('../views/reports/Reports.vue')
Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    children: [{
        path: '/',
        redirect: '/welcome'
      },
      {
        path: '/welcome',
        name: 'welcome',
        component: Welcome
      },
      {
        path: '/users',
        name: 'user',
        component: User
      },
      {
        path: '/roles',
        name: 'HomeRoles',
        component: HomeRoles
      },
      {
        path: '/rights',
        name: 'HomeRights',
        component: HomeRights
      },
      {
        path: '/categories',
        name: 'GoodsCategory',
        component: GoodsCategory
      },
      {
        path: '/goods',
        name: 'GoodsLists',
        component: GoodsLists,
      },
      {
        path: '/add',
        name: 'AddGoodsList',
        component: AddGoodsList,
      },
      {
        path: '/params',
        name: 'goodsParams',
        component: goodsParams
      },
      {
        path: '/orders',
        name: 'Orders',
        component: Orders
      },
      {
        path: '/reports',
        name: 'Reports',
        component: Reports
      },

    ]
  },
  {
    path: '/about',
    name: 'About',
    component: () => import( /* webpackChunkName: "about" */ '../views/About.vue')
  },

]

const router = new VueRouter({
  routes,
  mode: "history"
})
//创建导航守卫,当token存在时才能进行后续操作
router.beforeEach((to, from, next) => {
  //允许用户进入登录页面
  if (to.path === '/login') return next();
  if (!window.sessionStorage.token) return next('/login')
  next()
})

export default router